<template>
	<view class="safe-area-inset-bottom">
		<view class="swip_header">
			<view class="slot-box">
				<view style="width: 100%;">
					<uni-search-bar v-model="queryParams.name" type="text" @confirm="handleQuery" @input="handleQuery" placeholder="请输入类型名称" />
				</view>
			</view>
			<view class="swiplist">
				<view>签署流程设置</view>
				<button size="mini" class="add_btn" @click="handleAdd">
				<i class="iconfont" style="color: #00a660;">&#xeaf3;</i>新增</button>
			</view>
		</view>
		<!-- 列表 -->
		<view v-if="signProcessList != ''" class="listNameTop cardList">
		<uni-card :is-shadow="false" v-for="(item,index) in signProcessList" :key="index">	
				<view class="flex_between">
					<p class="grid-hidden" style="width: 70%;">签章流程名称：{{ item.name }}</p>
					<switch color="#00a660" style="transform:scale(0.7)" :checked="item.status == 1 ?true:false"
						@change="switchChange(item)" v-if="item.isSystem == 1" disabled />
					<span v-else @click="switchChange(item)">
						<switch color="#00a660" style="transform:scale(0.7)" :checked="item.status == 1 ?true:false" disabled />
					</span>
				</view>
				<p class="grid-integral">签署流程类型： 
				<span v-if="item.contractSignFileType==0" style="color:orange">标准合同</span>
				<span v-if="item.contractSignFileType==1" style="color:#00a660">普通合同</span>
				</p>
			<p class="grid-integral">发起人：
			<span v-if="item.starterNameList != null">
				<span v-for="flow in item.starterNameList">{{flow}}，</span>
			</span>
			<span v-else>暂无</span>
			</p>
			<p class="grid-integral">签署人：
			<span v-if="item.sealerNameList != null">
				<span v-for="flow in item.sealerNameList">{{flow}}，</span>
			</span>
			<span v-else>暂无</span>
			</p>
			<p class="grid-integral">流程管理员：
			<span v-if="item.manageNames != null">
				<span v-for="flow in item.manageNames">{{flow}}，</span>
			</span>
			<span v-else>暂无</span>
			</p>
			<p class="grid-integral">流程创建时间：{{item.createTime}}</p>
			<p class="grid-integral" v-if="item.updateTime != null">流程更新时间：{{item.updateTime}}</p>
			<p class="grid-integral" v-else>流程更新时间：--</p>
			<view slot="actions" class="card-actions">
			  <view class="grid-integral">合同类型：{{ item.contractTypeName }}</view>
			  <view class="flex_between">
			  <view class="card-actions-item" @click="handleEdit(item)">
			    <text class="card-actions-item-text themeColor">编辑</text>
			  </view>
			  <view class="card-actions-item" @click="switchChange(item,index)" v-if="item.isSystem == 0 && item.status == 1">
			    <text class="card-actions-item-text themeColor">停用</text>
			  </view>
			  <view class="card-actions-item" @click="handleDelete(item,index)">
			    <text class="card-actions-item-text errorColor">删除</text>
			  </view>
			  </view>
			</view>	
		</uni-card>			
		<!-- 加载更多 -->
		<uni-load-more color="#00a660" :status="loadStatus" />
		</view>
		<!-- 暂无数据 -->
		<luanqingEmpty :show="true" text="没有更多数据啦" v-else />
	</view>
</template>

<script>
	import {
	  addSignProcess, delSignProcess,
	  getSignProcess, listSignProcess,
	  updateSignProcess, updateSignProcessStatus
	} from "@/api/ec/console/busiConfig/contractSignProcess";
	import {uniqueArr} from "@/utils";
	import luanqingEmpty from '@/components/luanqing-empty/luanqing-empty';
	import {parseTime} from "@/utils/common";
	export default {
		components:{luanqingEmpty,},
		data() {
			return {
				total:0, //总条数
				loadStatus:'loading', //是否加载中 //加载样式：more-加载前样式，loading-加载中样式，nomore-没有数据样式
				signProcessList: [], // 合同类型列表
				// 查询参数
				queryParams: {
				  pageNum: 1,
				  pageSize: 10,
				  name: null,
				  isSystem: null,
				  status:[],
				},
			}
		},
		created() {
			this.getList()
			// 从详情页返回该页面的刷新数据
			uni.$on('refreshData',() => {
				this.getList(true);
			})	
				
		},
		//上拉触底函数
		onReachBottom() {
		  if (this.loadStatus != 'noMore' && this.total > this.signProcessList.length) {
		    this.loadStatus = 'loading';
		    // 延迟500毫秒显示loading效果
		    setTimeout(() => {
		      // 执行查询的方法
		      this.getList(false);
		    }, 500)
		  } else {
		    // 停止加载
		    this.loadStatus = 'noMore'
		  }
		},
		methods: {
			/** 添加流程设置 */
			handleAdd(){
				this.$tab.navigateTo('/pages/mine/work/busiConfig/signingProcess/processSet')
			},
			/** 搜索 */
			handleQuery(){
				this.getList(true);      
			},
			/** 获取在线模板列表 */
			getList(reload) {
			  if (reload) {
			    this.queryParams.pageNum = 1;
			    this.loadStatus = 'more'
                this.$modal.loading();
			  }
			  listSignProcess(this.queryParams).then(response => {
			    if (reload) {
			      uni.hideLoading()
			    }
			    this.total = response.total;
			    if (response.total > 0) {
			      const dataRows = response.rows;
			      this.signProcessList = reload ? dataRows : this.signProcessList.concat(dataRows);
			    } else {
			      this.signProcessList = [];
			    }
			    // 判断数据是否加载完全
			    if (response.total <= this.signProcessList.length) {
			      this.loadStatus = 'noMore'
			    }
			    // 页数+1
			    this.queryParams.pageNum++
			  }).catch(() => {
			    if (reload) {
			      uni.hideLoading()
			    }
			  });
			},
			// status == 0停用 1启用
			switchChange(row){
				let that = this
				let text = row.status === 1 ? "停用" : "启用";
				uni.showModal({
					title:'系统提示',
					content:'确认要 " ' + text + ' " 吗？',
					conformColor:'#00a660',
					success:function(res){
						if(res.confirm){
							let data = {id: row.id, status: row.status === 1?0:1};
							updateSignProcessStatus(data);
							that.getList()
							that.$modal.msgSuccess(text + "成功");
							row.status === 0 ? "停用" : "启用";
						}
					}
				})
			},
			/** 编辑 */
			handleEdit(item,index){
				uni.navigateTo({
					url:'./processSet?id='+item.id
				})	
			},
			/** 删除签署流程 */
			handleDelete(row,index){
				let that = this
				uni.showModal({
					title:'系统提示',
					content:'是否确认删除该签署流程？',
					conformColor:'#00a660',
					success:function(res){
						if(res.confirm){
							let ids = row.id;
						    delSignProcess(ids);
							that.signProcessList.splice(index,1)
				            that.$modal.msgSuccess("删除成功");
						}
					}
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	::v-deep .uni-forms-item__label{width: 150rpx !important;}
	::v-deep .uni-popup .uni-popup__wrapper{width: 90% !important;padding:0;}
	
	.more_option{
		font-size: 25rpx;
		color: #999999;
		.iconfont{
			font-size: 20rpx;
			padding: 5rpx;
		}
	}
</style>
